/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import server.Server;

/**
 *
 * @author casshern
 */
public class CreateGameFrame extends JFrame implements ActionListener {

    private JButton start;
    private JLabel label;
    private Server server;
    private Client client;

    public CreateGameFrame() {
        start = new JButton("Start");
        start.setEnabled(false);//not enabled the start button
        start.setPreferredSize(new Dimension(100, 80));
        start.addActionListener(this);
        label = new JLabel();
        label.setText("Waiting other plays....");

        label.setPreferredSize(new Dimension(150, 80));
        this.add(start, BorderLayout.CENTER);
        this.add(label, BorderLayout.CENTER);

        this.setLayout(new FlowLayout());
        this.setLocation(400, 200);
        this.setSize(500, 150);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        try {
            server = new Server(this);
            client = new Client("localhost");
            client.setID(1);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Creating server failed.");
            System.exit(1);
        }
    }

    public void setStatus() {
        start.setEnabled(true);
        label.setText("You can start the game.");
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == start) {
            try {
                server.gameBegin();
                this.setVisible(false);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Something wrong with IOStream.");
                System.exit(1);
            }
        }
    }
}
